package com.itheima.mapper;

import com.itheima.pojo.Emp;
import org.apache.ibatis.annotations.*;

import java.time.LocalDate;
import java.util.List;

@Mapper
//生成代理对象，并放入IOC容器之中
public interface EmpMapper {

    //删除员工
    @Delete("delete from emp where id = #{id}")
    public void delete(Integer id);
    //public int delete(Integer id);

    //新增员工
    @Options(keyProperty = "id", useGeneratedKeys = true) //获取返回的主键
    @Insert("insert ignore into emp(username, name, gender, image, job, entrydate, dept_id, create_time, update_time) " +
            "values (#{username}, #{name},#{gender},#{image},#{job},#{entrydate},#{deptId},#{createTime},#{updateTime})")
    public void insert(Emp emp);

    //更新员工
    @Update("update emp set username = #{username}, name = #{name}, gender = #{gender}," +
            " image = #{image}, job = #{job}, entrydate = #{entrydate}, dept_id = #{deptId}," +
            " update_time = #{updateTime} where id = #{id}")
    public void update(Emp emp);

    //根据ID查询员工
//    @Select("select * from emp where id = #{id}")
//    public Emp getById(Integer id);


    //为了让mybatis自动封装所有的属性，需要让字段与实体类属性名保持一致

    //方案三：开始mybatis的驼峰命名自动映射开关
    //配置properties文件中的内容
    //mybatis.configuration.map-underscore-to-camel-case=true
    @Select("select * from emp where id = #{id}")
    public Emp getById(Integer id);



    //方案一；给字段起别名,让别名与实体类属性保持一致
//    @Select("select id, username, password, name, gender, image, job," +
//            " entrydate, dept_id deptId, create_time createTime, update_time updateTime from emp where id = #{id}")
//    public Emp getById(Integer id);

    //方案二：通过@Results，@Result注解手动映射封装
//    @Results({
//            @Result(column = "dept_id", property = "deptId"),
//            @Result(column = "create_time", property = "createTime"),
//            @Result(column = "update_time", property = "updateTime")
//
//    })
//    @Select("select * from emp where id = #{id}")
//    public Emp getById(Integer id);

    //条件查询员工
//    @Select("select * from emp where name like '%${name}%' and gender = #{gender} and " +
//            "entrydate between #{begin} and #{end} order by update_time desc")
//    public List<Emp> list(String name, Short gender, LocalDate begin, LocalDate end);

    //没有参数占位符：#{} 生成的就不是预编译的SQL 也就会存在SQL注入问题

    //使用concat解决字符串拼接问题
//    @Select("select * from emp where name like concat('%',#{name} ,'%') and gender = #{gender} and " +
//            "entrydate between #{begin} and #{end} order by update_time desc")
//    public List<Emp> list(String name, Short gender, LocalDate begin, LocalDate end);
//

    //利用xml配置文件形式来配置sql语句
//    public List<Emp> list(String name, Short gender, LocalDate begin, LocalDate end);

    //动态条件查询
    public List<Emp> list(String name, Short gender, LocalDate begin, LocalDate end);

    //动态更新员工信息
    public void update2(Emp emp);

    //批量删除员工
    public void deleteByIds(List<Integer> ids);
}
